<?php

//	Author: 	Aris Michail
//	Email:		std03055@di.uoa.gr
//	Purpose: 	Get the values of the attributes of an entity

// REVISE attributes table on entity

require_once ("../DB/DB.php");
require_once ("../Frameworks/Objects/user.php");
require_once ("../Frameworks/Objects/category.php");

function DB_category_getentitieswithvalues($categoryowner, $category, &$entities)
{

    global $conn;

    // The name of the stored procedure
    $sp_name = "usp_category_getentitieswithvalues";

    // The return variable
    $retval;

    // Get the values
    $owner = $categoryowner->GetUsername();
    $catname = $category->GetName();
    $entityname = '%';

    // Create a new statement
    $stmt = mssql_init($sp_name, $conn);

    // Bind values
    mssql_bind($stmt, "RETVAL", &$retval, SQLINT4, true, false, 1);
    mssql_bind($stmt, '@ownername', &$owner, SQLVARCHAR, false, false, 50);
    mssql_bind($stmt, '@categoryname', &$catname, SQLVARCHAR, false, false, 50);
    mssql_bind($stmt, '@entityname', &$entityname, SQLVARCHAR, false, false, 50);

    // Execute the statement
    $status = mssql_execute($stmt);
    if (!$status)
        return DB_ERROR_CANNOTGETVALUESFORENTITY;
    else
    {
        /* the table that has all entities */
        $myentities = array();
        /* the table with the attributes of the category */
        $attributes = array();
        /* the table with the names of all entities in category */
        $entitynames = array();
        /* get the attributes of the category */
        $rvalue = DB_category_getattributes($categoryowner, $category, &$attributes);

        if ($rvalue != 0)
            return $rvalue;

        /* get the names of the entities in category */
        $rvalue = DB_category_getentitynames($categoryowner, $category, &$entitynames);

        if ($rvalue != 0)
            return $rvalue;

        foreach ($entitynames as & $entityname)
        {
             /* create a table of entities that only have names and attributes */
            $entity = new Entity();
             /* but not values */
            $entity->SetName($entityname);
            $entity->SetAttributes($attributes);
        }

        $i = '0';
        while ($row = mssql_fetch_row($status))
        {
            foreach ($entities as & $entity)
            {
                if ($entity->GetName() == $row[0]) /* update the entity */
                    foreach ($attributes as & $attribute)
                    {
                        if ($attribute->GetName() == $row[1]) /* update the value of the attribute */
                            $attribute->SetValue($row[2]);
                    }
            }

            $i++;
            mssql_next_result($status);
        }
        return DB_OK;
    }
}

?>